package com.ict.txmsv2.services;

import com.ict.txmsv2.dto.StudentInfoQueryDto;
import com.ict.txmsv2.mapper.StudentInfoMapper;
import com.ict.txmsv2.model.RespBean;
import com.ict.txmsv2.model.StudentInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author lht
 * @date 2021/4/21 - 21:13
 * @description: 工作室学生业绩信息业务类
 */
@Service
public class StudentInfoService {

    @Resource
    private StudentInfoMapper studentInfoMapper;

    /**
     * 上传文件到数据库
     * @param file
     * @return
     */
    public boolean uploadFile(MultipartFile file) {
        StudentInfo info = new StudentInfo();
        try {
            info.setCreateTime(LocalDateTime.now());
            info.setUpdateTime(LocalDateTime.now());
            info.setFile(file.getBytes());
            info.setFileName(file.getOriginalFilename());
        }catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return studentInfoMapper.uploadFile(info) != 0;
    }

    /**
     * 通过文件的id删除文件
     * @param id
     * @return
     */
    public boolean deleteFileById(Integer id) {
        return studentInfoMapper.deleteFileById(id) != 0;
    }


    public RespBean fuzzyGetStudentInfoList(StudentInfoQueryDto queryDto) {
        List<StudentInfo> studentInfos = studentInfoMapper.fuzzyGetStudentInfoList(queryDto);
        return RespBean.ok("", studentInfos);
    }
}
